/*     */ package com.zimbra.cs.volume;
/*     */ 
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.soap.SoapTransport;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.cs.util.BuildInfo;
/*     */ import com.zimbra.cs.util.SoapCLI;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.admin.message.CreateVolumeRequest;
/*     */ import com.zimbra.soap.admin.message.CreateVolumeResponse;
/*     */ import com.zimbra.soap.admin.message.DeleteVolumeRequest;
/*     */ import com.zimbra.soap.admin.message.GetAllVolumesRequest;
/*     */ import com.zimbra.soap.admin.message.GetAllVolumesResponse;
/*     */ import com.zimbra.soap.admin.message.GetVolumeRequest;
/*     */ import com.zimbra.soap.admin.message.GetVolumeResponse;
/*     */ import com.zimbra.soap.admin.message.ModifyVolumeRequest;
/*     */ import com.zimbra.soap.admin.message.SetCurrentVolumeRequest;
/*     */ import com.zimbra.soap.admin.type.VolumeInfo;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.Option;
/*     */ import org.apache.commons.cli.OptionGroup;
/*     */ import org.apache.commons.cli.Options;
/*     */ import org.apache.commons.cli.ParseException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class VolumeCLI
/*     */   extends SoapCLI
/*     */ {
/*     */   private static final String O_A = "a";
/*     */   private static final String O_D = "d";
/*     */   private static final String O_L = "l";
/*     */   private static final String O_E = "e";
/*     */   private static final String O_DC = "dc";
/*     */   private static final String O_SC = "sc";
/*     */   private static final String O_TS = "ts";
/*     */   private static final String O_ID = "id";
/*     */   private static final String O_T = "t";
/*     */   private static final String O_N = "n";
/*     */   private static final String O_P = "p";
/*     */   private static final String O_C = "c";
/*     */   private static final String O_CT = "ct";
/*     */   private ZAuthToken auth;
/*     */   private String id;
/*     */   private String type;
/*     */   private String name;
/*     */   private String path;
/*     */   private String compress;
/*     */   private String compressThreshold;
/*     */   
/*     */   private VolumeCLI()
/*     */     throws ServiceException
/*     */   {
/*  65 */     setupCommandLineOptions();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void setArgs(CommandLine cl)
/*     */     throws ServiceException, ParseException, IOException
/*     */   {
/*  77 */     this.auth = getZAuthToken(cl);
/*  78 */     this.id = cl.getOptionValue("id");
/*  79 */     this.type = cl.getOptionValue("t");
/*  80 */     this.name = cl.getOptionValue("n");
/*  81 */     this.path = cl.getOptionValue("p");
/*  82 */     this.compress = cl.getOptionValue("c");
/*  83 */     this.compressThreshold = cl.getOptionValue("ct");
/*     */   }
/*     */   
/*     */   public static void main(String[] args) {
/*  87 */     CliUtil.toolSetup();
/*  88 */     SoapTransport.setDefaultUserAgent("zmvolume", BuildInfo.VERSION);
/*  89 */     VolumeCLI util = null;
/*     */     try {
/*  91 */       util = new VolumeCLI();
/*     */     } catch (ServiceException e) {
/*  93 */       System.err.println(e.getMessage());
/*  94 */       System.exit(1);
/*     */     }
/*     */     try {
/*  97 */       CommandLine cl = util.getCommandLine(args);
/*  98 */       if (cl == null) {
/*  99 */         return;
/*     */       }
/* 101 */       util.setArgs(cl);
/*     */       
/* 103 */       if (cl.hasOption("a")) {
/* 104 */         util.addVolume();
/* 105 */       } else if (cl.hasOption("d")) {
/* 106 */         util.deleteVolume();
/* 107 */       } else if (cl.hasOption("l")) {
/* 108 */         util.getVolume();
/* 109 */       } else if (cl.hasOption("e")) {
/* 110 */         util.editVolume();
/* 111 */       } else if (cl.hasOption("dc")) {
/* 112 */         util.getCurrentVolumes();
/* 113 */       } else if (cl.hasOption("sc")) {
/* 114 */         util.setCurrentVolume();
/* 115 */       } else if (cl.hasOption("ts")) {
/* 116 */         util.unsetCurrentSecondaryMessageVolume();
/*     */       } else {
/* 118 */         throw new ParseException("No action (-a,-d,-l,-e,-dc,-sc,-ts) is specified");
/*     */       }
/* 120 */       System.exit(0);
/*     */     } catch (ParseException e) {
/* 122 */       util.usage(e);
/*     */     } catch (Exception e) {
/* 124 */       System.err.println("Error occurred: " + e.getMessage());
/*     */     }
/* 126 */     System.exit(1);
/*     */   }
/*     */   
/*     */   private void setCurrentVolume() throws ParseException, SoapFaultException, IOException, ServiceException {
/* 130 */     if (this.id == null) {
/* 131 */       throw new ParseException("id is missing");
/*     */     }
/*     */     
/* 134 */     auth(this.auth);
/* 135 */     GetVolumeResponse resp = (GetVolumeResponse)JaxbUtil.elementToJaxb(getTransport().invokeWithoutSession(JaxbUtil.jaxbToElement(new GetVolumeRequest(Short.valueOf(this.id).shortValue()))));
/*     */     
/* 137 */     VolumeInfo vol = resp.getVolume();
/* 138 */     SetCurrentVolumeRequest req = new SetCurrentVolumeRequest(vol.getId(), vol.getType());
/* 139 */     getTransport().invokeWithoutSession(JaxbUtil.jaxbToElement(req));
/*     */     
/* 141 */     System.out.println("Volume " + this.id + " is now the current " + toTypeName(vol.getType()) + " volume.");
/*     */   }
/*     */   
/*     */   private void unsetCurrentSecondaryMessageVolume() throws SoapFaultException, IOException, ServiceException {
/* 145 */     SetCurrentVolumeRequest req = new SetCurrentVolumeRequest((short)-2, (short)2);
/* 146 */     auth(this.auth);
/* 147 */     getTransport().invokeWithoutSession(JaxbUtil.jaxbToElement(req));
/* 148 */     System.out.println("Turned off the current secondary message volume.");
/*     */   }
/*     */   
/*     */   private void getCurrentVolumes() throws SoapFaultException, IOException, ServiceException {
/* 152 */     GetAllVolumesRequest req = new GetAllVolumesRequest();
/* 153 */     auth(this.auth);
/* 154 */     GetAllVolumesResponse all = (GetAllVolumesResponse)JaxbUtil.elementToJaxb(getTransport().invokeWithoutSession(JaxbUtil.jaxbToElement(req)));
/*     */     
/* 156 */     for (VolumeInfo vol : all.getVolumes()) {
/* 157 */       if (vol.isCurrent().booleanValue()) {
/* 158 */         print(vol);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private void getVolume() throws IOException, ServiceException {
/* 164 */     if (this.id == null) {
/* 165 */       GetAllVolumesRequest req = new GetAllVolumesRequest();
/* 166 */       auth(this.auth);
/* 167 */       GetAllVolumesResponse resp = (GetAllVolumesResponse)JaxbUtil.elementToJaxb(getTransport().invokeWithoutSession(JaxbUtil.jaxbToElement(req)));
/*     */       
/* 169 */       for (VolumeInfo vol : resp.getVolumes()) {
/* 170 */         print(vol);
/*     */       }
/*     */     } else {
/* 173 */       GetVolumeRequest req = new GetVolumeRequest(Short.parseShort(this.id));
/* 174 */       auth(this.auth);
/* 175 */       GetVolumeResponse resp = (GetVolumeResponse)JaxbUtil.elementToJaxb(getTransport().invokeWithoutSession(JaxbUtil.jaxbToElement(req)));
/*     */       
/* 177 */       print(resp.getVolume());
/*     */     }
/*     */   }
/*     */   
/*     */   private void print(VolumeInfo vol) {
/* 182 */     System.out.println("   Volume id: " + vol.getId());
/* 183 */     System.out.println("        name: " + vol.getName());
/* 184 */     System.out.println("        type: " + toTypeName(vol.getType()));
/* 185 */     System.out.println("        path: " + vol.getRootPath());
/* 186 */     System.out.print("  compressed: " + vol.isCompressBlobs());
/* 187 */     if (vol.isCompressBlobs()) {
/* 188 */       System.out.println("\t         threshold: " + vol.getCompressionThreshold() + " bytes");
/*     */     } else {
/* 190 */       System.out.println();
/*     */     }
/* 192 */     System.out.println("     current: " + vol.isCurrent());
/* 193 */     System.out.println();
/*     */   }
/*     */   
/*     */   private void deleteVolume() throws ParseException, SoapFaultException, IOException, ServiceException {
/* 197 */     if (this.id == null) {
/* 198 */       throw new ParseException("id is missing");
/*     */     }
/*     */     
/* 201 */     DeleteVolumeRequest req = new DeleteVolumeRequest(Short.parseShort(this.id));
/* 202 */     auth(this.auth);
/* 203 */     getTransport().invokeWithoutSession(JaxbUtil.jaxbToElement(req));
/* 204 */     System.out.println("Deleted volume " + this.id);
/*     */   }
/*     */   
/*     */   private void editVolume() throws ParseException, SoapFaultException, IOException, ServiceException {
/* 208 */     if (Strings.isNullOrEmpty(this.id)) {
/* 209 */       throw new ParseException("id is missing");
/*     */     }
/*     */     
/* 212 */     VolumeInfo vol = new VolumeInfo();
/* 213 */     if (!Strings.isNullOrEmpty(this.type)) {
/* 214 */       vol.setType(toType(this.type));
/*     */     }
/* 216 */     if (!Strings.isNullOrEmpty(this.name)) {
/* 217 */       vol.setName(this.name);
/*     */     }
/* 219 */     if (!Strings.isNullOrEmpty(this.path)) {
/* 220 */       vol.setRootPath(this.path);
/*     */     }
/* 222 */     if (!Strings.isNullOrEmpty(this.compress)) {
/* 223 */       vol.setCompressBlobs(Boolean.valueOf(Boolean.parseBoolean(this.compress)));
/*     */     }
/* 225 */     if (!Strings.isNullOrEmpty(this.compressThreshold)) {
/* 226 */       vol.setCompressionThreshold(Long.parseLong(this.compressThreshold));
/*     */     }
/* 228 */     ModifyVolumeRequest req = new ModifyVolumeRequest(Short.parseShort(this.id), vol);
/* 229 */     auth(this.auth);
/* 230 */     getTransport().invokeWithoutSession(JaxbUtil.jaxbToElement(req));
/* 231 */     System.out.println("Edited volume " + this.id);
/*     */   }
/*     */   
/*     */   private void addVolume() throws ParseException, SoapFaultException, IOException, ServiceException {
/* 235 */     if (this.id != null) {
/* 236 */       throw new ParseException("id cannot be specified when adding a volume");
/*     */     }
/* 238 */     if (Strings.isNullOrEmpty(this.type)) {
/* 239 */       throw new ParseException("type is missing");
/*     */     }
/* 241 */     if (Strings.isNullOrEmpty(this.name)) {
/* 242 */       throw new ParseException("name is missing");
/*     */     }
/* 244 */     if (Strings.isNullOrEmpty(this.path)) {
/* 245 */       throw new ParseException("path is missing");
/*     */     }
/*     */     
/* 248 */     VolumeInfo vol = new VolumeInfo();
/* 249 */     vol.setType(toType(this.type));
/* 250 */     vol.setName(this.name);
/* 251 */     vol.setRootPath(this.path);
/* 252 */     vol.setCompressBlobs(Boolean.valueOf(this.compress != null ? Boolean.parseBoolean(this.compress) : false));
/* 253 */     vol.setCompressionThreshold(this.compressThreshold != null ? Long.parseLong(this.compressThreshold) : 4096L);
/* 254 */     CreateVolumeRequest req = new CreateVolumeRequest(vol);
/* 255 */     auth();
/* 256 */     CreateVolumeResponse resp = (CreateVolumeResponse)JaxbUtil.elementToJaxb(getTransport().invokeWithoutSession(JaxbUtil.jaxbToElement(req)));
/*     */     
/* 258 */     System.out.println("Volume " + resp.getVolume().getId() + " is created");
/*     */   }
/*     */   
/*     */   protected String getCommandUsage()
/*     */   {
/* 263 */     return "zmvolume {-a | -d | -l | -e | -dc | -sc } <options>";
/*     */   }
/*     */   
/*     */   protected void setupCommandLineOptions()
/*     */   {
/* 268 */     super.setupCommandLineOptions();
/* 269 */     Options options = getOptions();
/* 270 */     OptionGroup og = new OptionGroup();
/* 271 */     og.addOption(new Option("a", "add", false, "Adds a volume."));
/* 272 */     og.addOption(new Option("d", "delete", false, "Deletes a volume."));
/* 273 */     og.addOption(new Option("l", "list", false, "Lists volumes."));
/* 274 */     og.addOption(new Option("e", "edit", false, "Edits a volume."));
/* 275 */     og.addOption(new Option("dc", "displayCurrent", false, "Displays the current volumes."));
/* 276 */     og.addOption(new Option("sc", "setCurrent", false, "Sets the current volume."));
/* 277 */     og.addOption(new Option("ts", "turnOffSecondary", false, "Turns off the current secondary message volume"));
/* 278 */     og.setRequired(true);
/* 279 */     options.addOptionGroup(og);
/* 280 */     options.addOption("id", "id", true, "Volume ID");
/* 281 */     options.addOption("t", "type", true, "Volume type (primaryMessage, secondaryMessage, or index)");
/* 282 */     options.addOption("n", "name", true, "volume name");
/* 283 */     options.addOption("p", "path", true, "Root path");
/* 284 */     options.addOption("c", "compress", true, "Compress blobs; \"true\" or \"false\"");
/* 285 */     options.addOption("ct", "compressionThreshold", true, "Compression threshold; default 4KB");
/* 286 */     options.addOption(SoapCLI.OPT_AUTHTOKEN);
/* 287 */     options.addOption(SoapCLI.OPT_AUTHTOKENFILE);
/*     */   }
/*     */   
/*     */   protected void usage(ParseException e)
/*     */   {
/* 292 */     if (e != null) {
/* 293 */       System.err.println("Error parsing command line arguments: " + e.getMessage());
/*     */     }
/*     */     
/* 296 */     System.err.println(getCommandUsage());
/* 297 */     printOpt("a", 0);
/* 298 */     printOpt("n", 2);
/* 299 */     printOpt("t", 2);
/* 300 */     printOpt("p", 2);
/* 301 */     printOpt("c", 2);
/* 302 */     printOpt("ct", 2);
/* 303 */     printOpt("e", 0);
/* 304 */     printOpt("id", 2);
/* 305 */     System.err.println("  any of the options listed under -a can also be specified ");
/* 306 */     System.err.println("  to have its value modified.");
/* 307 */     printOpt("d", 0);
/* 308 */     printOpt("id", 2);
/* 309 */     printOpt("l", 0);
/* 310 */     printOpt("id", 2);
/* 311 */     System.err.println("  -id is optional.");
/* 312 */     printOpt("dc", 0);
/* 313 */     printOpt("sc", 0);
/* 314 */     printOpt("id", 2);
/* 315 */     printOpt("ts", 0);
/* 316 */     printOpt("y", 0);
/* 317 */     printOpt("Y", 0);
/*     */   }
/*     */   
/*     */   private void printOpt(String optStr, int leftPad) {
/* 321 */     Options options = getOptions();
/* 322 */     Option opt = options.getOption(optStr);
/* 323 */     StringBuilder buf = new StringBuilder();
/* 324 */     buf.append(Strings.repeat(" ", leftPad));
/* 325 */     buf.append('-').append(opt.getOpt()).append(",--").append(opt.getLongOpt());
/* 326 */     if (opt.hasArg()) {
/* 327 */       buf.append(" <arg>");
/*     */     }
/* 329 */     buf.append(Strings.repeat(" ", 35 - buf.length()));
/* 330 */     buf.append(opt.getDescription());
/* 331 */     System.err.println(buf.toString());
/*     */   }
/*     */   
/*     */ 
/*     */   private short toType(String name)
/*     */     throws ParseException
/*     */   {
/* 338 */     if ("primaryMessage".equalsIgnoreCase(name)) {
/* 339 */       return 1;
/*     */     }
/* 341 */     if ("secondaryMessage".equalsIgnoreCase(name)) {
/* 342 */       return 2;
/*     */     }
/* 344 */     if ("index".equalsIgnoreCase(name)) {
/* 345 */       return 10;
/*     */     }
/* 347 */     throw new ParseException("invalid volume type: " + name);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private String toTypeName(short type)
/*     */   {
/* 354 */     switch (type) {
/*     */     case 1: 
/* 356 */       return "primaryMessage";
/*     */     case 2: 
/* 358 */       return "secondaryMessage";
/*     */     case 10: 
/* 360 */       return "index";
/*     */     }
/* 362 */     return "Unrecognized type " + type;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/volume/VolumeCLI.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */